<?php
class javascript_Core
{
	public static $php_var = array();
	public static $pure_js = '';
	public static $js_file = '';

	/**
	 * add php variable to javascript
	 * @param array|string pare value . eg:array('test'=>'yoyo')
	 */
	public static function add_php_var($array)
	{
		//if a pare of string is provided , convert to array
		if(count(func_get_args())==2)
		{
			$funcs = func_get_args();
			$array = array($funcs[0]=>$funcs[1]);
		}
		self::$php_var = array_merge(self::$php_var,$array);
	}

	/**
	 * output php variable to js
	 */
	public static function render()
	{
		$js_str = self::$js_file;
		$js_str .= '<script type="text/javascript">'."\n";
		$php_var = self::$php_var;
		//add variable to body
		if(!empty($php_var))
		{
			foreach($php_var as $key=>$val)
			{
				$js_str .= '$("body").data("'.$key.'","'.$val.'");'."\n";
			}
		}
		$js_str .= self::$pure_js;
		$js_str .= '</script>';
		Event::$data = str_ireplace('<!--output_script-->',$js_str,Event::$data);
	}

	public static function add_pure_js($js)
	{
		self::$pure_js .= $js;
	}

	public function add_js_file($file)
	{
		self::$js_file .= $file;
	}
	
}
